#include "daovocabulary.h"

#include <QtSql>

#include "sql.h"
#include "utildb.h"

void VocabularyDao::insert(const Vocabulary vocabulary)
{
    DbUtil& dbUtil = DbUtil::getInstance();
    QSqlQuery query(dbUtil.getDatabase());
    query.prepare(SQL_INSERT_VOCABULARY);

    query.addBindValue(QString::fromStdString(vocabulary.vocabularyName()));
    query.addBindValue(vocabulary.vocabularySize());

    query.exec();
    dbUtil.close();

}

Vocabulary VocabularyDao::findById(int id)
{
    Vocabulary vocabulary;

    DbUtil& dbUtil = DbUtil::getInstance();
    QSqlQuery query(dbUtil.getDatabase());

    query.prepare(SQL_SELECT_VOCABULAEY);
    query.addBindValue(id);
    query.exec();
    if (query.exec()) {
        vocabulary.parseRecord(query.record());
    }
    dbUtil.close();
    return vocabulary;
}

QList<Vocabulary> VocabularyDao::queryAll()
{
    QList<Vocabulary> vocabularys;
    DbUtil& dbUtil = DbUtil::getInstance();
    QSqlQuery query(dbUtil.getDatabase());
    query.exec(SQL_SELECT_VOCABULARY_ALL);

    Vocabulary *vocabulary;
    while (query.next()) {
        vocabulary = new Vocabulary();
        vocabulary->parseRecord(query.record());
        vocabularys.append(*vocabulary);
    }

    dbUtil.close();
    return vocabularys;
}

VocabularyDao::VocabularyDao()
{

}
